package br.com.salao.session;

import java.util.List;

import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.log4j.Logger;

import br.com.salao.ejb.dao.FuncionarioDao;
import br.com.salao.ejb.dao.impl.FuncionarioDaoImpl;
import br.com.salao.entity.Funcionario;

@Stateless
@Remote(FuncionarioBase.class)
public class FuncionarioBean implements FuncionarioBase {

	/**
	 * @author Dri
	 */
	private static final long serialVersionUID = 1L;
	private Logger logger = Logger.getLogger(FuncionarioBean.class.getName());

	@PersistenceContext(unitName = "SalaoBeleza")
	private EntityManager em;

	public FuncionarioBean(){
		//em =  JpaUtil.getEntityManager();
	}

	@TransactionAttribute
	public Funcionario incluir(Funcionario funcionario){

		FuncionarioDao dao = new FuncionarioDaoImpl(em);

		try{						
			return dao.inserir(funcionario);

		}catch (Exception e) {

			StringBuffer sb = new StringBuffer();
			sb.append(e.getStackTrace() + "\n");
			sb.append(e.getCause() + "\n");
			sb.append(e.getMessage() + "\n");			
			logger.error(sb.toString());

			return null;
		}

	}

	@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
	public List<Funcionario> buscarFuncionariosPorIdEstabelecimento(int idEstabelecimento){

		FuncionarioDao dao = new FuncionarioDaoImpl(em);

		try{						
			return dao.buscarFuncionariosPorIdEstabelecimento(idEstabelecimento);

		}catch (Exception e) {

			StringBuffer sb = new StringBuffer();
			sb.append(e.getStackTrace() + "\n");
			logger.error(sb.toString());

			return null;
		}		
	}

	@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
	public Funcionario buscarFuncionariosPorId(int id){

		FuncionarioDaoImpl dao = new FuncionarioDaoImpl(em);

		try{						
			return dao.buscarFuncionariosPorId(id);

		}catch (Exception e) {

			StringBuffer sb = new StringBuffer();
			sb.append(e.getStackTrace() + "\n");
			logger.error(sb.toString());

			return null;
		}
	}

}
